package com.liziing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.*;
import com.liziing.mapper.*;
import com.liziing.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@SuppressWarnings("all")
@RestController
@RequestMapping("/classroom_Usage")
public class ClassroomUsageController {
    Gson gson =  new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new JsonSerializer<LocalDateTime>() {
        @Override
        public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }}).create();
    //教室使用情况
    @Autowired
    private ClassroomUsageMapper classroomUsageMapper;
    //教室信息
    @Autowired
    private ClassRoomMapper classRoomMapper;
    //用户信息
    @Autowired
    private UsersMapper usersMapper;
    //班级信息
    @Autowired
    private ClassInformationMapper classInformationMapper;
    //课程信息
    @Autowired
    private CourseMapper courseMapper;

    // 分页查询教室使用情况
    @PostMapping("/classroomUsages")
    public String getClassroomUsage(@RequestBody HashMap<String, Integer> data) {
        int page = data.get("page");
        int numberPerPage = data.get("number_per_page");
        QueryWrapper<ClassroomUsage> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.last(String.format("limit %s,%s", page * numberPerPage - numberPerPage, numberPerPage));
        List<ClassroomUsage> classroomUsageList = classroomUsageMapper.selectList(studentQueryWrapper);
        return gson.toJson(classroomUsageList);
    }

    // 获取教室情况总数
    @GetMapping("/get_ClassroomUsages_Count")
    public long getClassroomUsageCount() {
        Long count = classroomUsageMapper.selectCount(null);
        return count;
    }

    // 添加教室使用情况
    @PostMapping("/add")
    public void addClassroomUsage(@RequestBody ClassroomUsage ClassroomUsage) {
        classroomUsageMapper.insert(ClassroomUsage);
    }

    // 删除教室使用情况
    @PostMapping("/delete")
    public void deleteClassroomUsage(@RequestBody ClassroomUsage ClassroomUsage) {
        classroomUsageMapper.deleteById(ClassroomUsage);
    }

    // 修改教室使用情况
    @PostMapping("/update")
    public void updateClassroomUsage(@RequestBody ClassroomUsage ClassroomUsage) {
        classroomUsageMapper.updateById(ClassroomUsage);
    }

    // 获取教室使用率
    @GetMapping("/get_classroom_usage_rate")
    public String getClassroomUsageRate() {
        //获取班级ID、班级名称
        //获取教室ID、教室名称
        //获取教师ID、教师名称
        //获取课程ID、课程名称
        List<HashMap<String, Object>> classroomUsageRate =
                classroomUsageMapper.getClasRoomUsageRate();
        return gson.toJson(classroomUsageRate);
    }

    @GetMapping("/get_usage_list_data")
    public String getUsageListData() {
        Map<String, Object> listdata = new HashMap<>();
        //获取下拉列表数据
        List<HashMap<String, Object>> classroomUsages =classroomUsageMapper.getClasRoomUsageData();
        //获取班级ID、班级名称
        List<ClassInformation> classInformations = classInformationMapper.getClassInformation_List_Data();
        //获取教室ID、教室名称
        List<ClassRoom> classrooms = classRoomMapper.getClassRoom_List_Data();
        //获取用户ID、用户名称
        List<Users> users = usersMapper.getUsers_List_Data();
        //获取课程ID、课程名称
        List<CourseInformation> courses = courseMapper.getCourseInformation_List_Data();
        listdata.put("data", classroomUsages);
        listdata.put("classrooms", classrooms);
        listdata.put("users", users);
        listdata.put("classInformations", classInformations);
        listdata.put("courses", courses);
        return gson.toJson(listdata);
    }

}
